/**
 * Copyright (c) 2011 Nokia Corporation.
 * This is Qt GameEnabler's (http://projects.developer.nokia.com/qtgameenabler)
 * GameWindow with everything but renderingcode stripped away to keep the example
 * as simple as possible.
 *
 * NOTE, THIS VERSION IS HEAVILY MODIFIED TO IMPLEMENT GLES11 INTERFACE INSTEAD OF GLES2
 * FOR THE PURPOSE OF THIS DEMO-APPLICATION.
 *
 */

#ifndef GAMEWINDOW_H
#define GAMEWINDOW_H

#ifdef Q_WS_MAEMO_5
#include <QApplication>
#include <QMetaType>
#include <QtCore>
#include <QtGui>
#include <QTextStream>
#endif // Q_WS_MAEMO_5

#include <QWidget>
//#include <egl.h>
//#include <gl.h>
#include <EGL/egl.h>
#include <GLES/gl.h>


namespace GE {

class GameWindow : public QWidget
{
    Q_OBJECT

public:
    explicit GameWindow(QWidget *parent = 0);
    virtual ~GameWindow();

public:
    void create();
    void destroy();

    unsigned int getTickCount() const;
    float getFrameTime() const;
    float getFPS() const;

    void setHdOutput(bool onOff);
    bool hdConnected() const;

    int width();
    int height();

    // GL helper functions.
        GLuint glhelpLoadTexture( QString imageFile );

public slots:
    void pause();
    void resume();

protected: // Application callbacks, override these in your own derived class
    virtual void onCreate();
    virtual void onInitEGL();
    virtual void onFreeEGL();
    virtual void onDestroy();
    virtual void onRender();
    virtual void onPause();
    virtual void onResume();
    virtual void onUpdate(const float frameDelta);
    virtual void onSizeChanged(int width, int height);

protected: // From QWidget (and other base classes)
    bool eventFilter(QObject *object, QEvent *event);
    void paintEvent(QPaintEvent *event);
    virtual void resizeEvent(QResizeEvent *event);
    void timerEvent(QTimerEvent *event);

private slots:

private: // For internal functionality
    void createEGL();
    void reinitEGL();
    void render();
    bool testEGLError(const char* pszLocation);
    void cleanupAndExit(EGLDisplay eglDisplay);
    virtual EGLNativeWindowType getWindow();

#ifdef Q_OS_SYMBIAN
private: // From IHdmiConnectionObserver
    void hdmiConnected(bool connected);
#endif

protected: // Data
    // EGL variables
    EGLDisplay eglDisplay;
    EGLConfig eglConfig;
    EGLSurface eglSurface;
    EGLContext eglContext;

private: // Data
    // Time calculation
    unsigned int m_prevTime;
    unsigned int m_currentTime;
    float m_frameTime;
    float m_fps;
    bool m_paused;
    int m_timerId;

    // HD output
    bool m_hdConnected;

};

} // namespace GE

#endif // GAMEWINDOW_H
